home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianLib.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  306 lines

  1. #include "Scian.h"
  2. #include "ScianIDs.h"
  3. #ifdef GRAPHICS
  4. Cant do that, bucko!
  5. #endif
  6. #include "ScianTypes.h"
  7. #include "ScianNames.h"
  8. #include "ScianArrays.h"
  9. #include "ScianLists.h"
  10. #include "ScianSockets.h"
  11. #include "ScianLib.h"
  12. #include "ScianWindows.h"
  13. #include "ScianDatasets.h"
  14. #include "ScianGarbageMan.h"
  15. #include "ScianScripts.h"
  16. #include "ScianNetObjects.h"
  17.  
  18. /* definitions stolen from ScianMain.c */
  19. Bool runningRemote = false;
  20. real missingData = 1.1E37;
  21. real plusInf = 1E37;
  22. real minusInf = -1E37;
  23. Bool demoP = false;
  24. Bool scriptSelectP = false;
  25. Bool abortScriptP = true;
  26.  
  27. int LibRunning()
  28. {
  29.     return libRunning;
  30. }
  31.  
  32. double MyClock()
  33. {
  34.     struct tms buffer;
  35.  
  36.     return times(&buffer) / HEARTBEAT;
  37. }
  38.  
  39. #ifdef PROTO
  40. void InitLib(char *name)
  41. #else
  42. void InitLib(name)
  43. char *name;
  44. #endif
  45. {
  46.     if (name && strncmp(name, "", 3))
  47.     {
  48.     whatsMyName = malloc(strlen(name) + 1);
  49.     strcpy(whatsMyName, name);
  50.     }
  51.  
  52.     libRunning = true;
  53.     InitNames();
  54.     InitObjects();
  55.     InitArrays();
  56.     InitLists();
  57. #ifdef SOCKETS
  58.     InitSockets();
  59. #endif
  60.     InitTimers();
  61.     InitDatasets();
  62.     DoEnablePublication();
  63. }
  64. void InitStuff()
  65. {
  66.     /* stub to replace ScianMain.c:InitStuff */
  67. }
  68.  
  69. void KillLib()
  70. {
  71.     KillDatasets();
  72.     KillTimers();
  73. #ifdef SOCKETS
  74.     KillSockets();
  75. #endif
  76.     KillLists();
  77.     KillArrays();
  78.     KillObjects();
  79.     KillNames();
  80.     libRunning = false;
  81. }
  82.  
  83. void IdleLib()
  84. {
  85. #if 0
  86.     IdleAllConnections();
  87.     TrashDay();
  88. #else
  89.     LongIdleLib();
  90. #endif
  91. }
  92.  
  93. void LongIdleLib()
  94. {
  95.     int hak, idle;
  96.     double lastTime;
  97.  
  98.     hak = 0;
  99.     idle = false;
  100.  
  101.     lastTime = MyClock();
  102.  
  103.     while (!idle)
  104.     {
  105.     IdleAllConnections();
  106.     if (++hak > 100)
  107.     {
  108.         hak = 0;
  109.         TrashDay();
  110.     }
  111.  
  112.     if (lastTime + 5.0 < MyClock())
  113.     {
  114.         idle = AllConnectionsIdle(true);
  115.         lastTime = MyClock();
  116.     }
  117.     else
  118.     {
  119.         idle = AllConnectionsIdle(false);
  120.     }
  121.     }
  122. }
  123.  
  124. int AdvertiseInteger(theInt)
  125. int theInt;
  126. {
  127.     ObjPtr theIntObj;
  128.  
  129.     if (!libRunning)
  130.     {
  131.     fprintf(stderr, "AdvertiseInteger: SciAn library not initialized\n");
  132.     return;
  133.     }
  134.  
  135.     theIntObj = NewInt(theInt);
  136.     if (!theIntObj)
  137.     {
  138.     fprintf(stderr, "memory!\n");
  139.     exit (-1);
  140.     }
  141.  
  142.     if (AdvertiseObject(theIntObj))
  143.     {
  144. #if 0
  145.     return GetInt(GetVar(theIntObj, NETWORKID));
  146. #else
  147.     return GetInt(GetVar(theIntObj, 998));
  148. #endif
  149.     }
  150.     return 0;
  151. }
  152.  
  153. #ifdef PROTO
  154. int AdvertiseCArray(char *name, real *arr, int rank, ...)
  155. #else
  156. int AdvertiseCArray(name, arr, rank)
  157. real *arr;
  158. int rank;
  159. char *name;
  160. #endif
  161. {
  162.     long *dimPtr;
  163.     ObjPtr dataForm, dimsArray, data, boundsArray, theDataset;
  164.     real dims[23];
  165.     real bounds[23 * 2];
  166.     int i;
  167.  
  168.     if (!libRunning)
  169.     {
  170.     fprintf(stderr, "AdvertiseCArray: SciAn library not initialized\n");
  171.     return 0;
  172.     }
  173.  
  174.     if (rank < 2 || rank > 3)
  175.     {
  176.     fprintf(stderr, "rank smell!\n");
  177.     return 0;
  178.     }
  179.  
  180.     dataForm = NewObject(dataFormClass, 0);
  181.     dimsArray = NewRealArray(1, (long) rank);
  182.     if (!dataForm || !dimsArray)
  183.     {
  184.     fprintf(stderr, "memory!\n");
  185.     exit (-1);
  186.     }
  187.  
  188.     /*Put in some dimensions*/
  189.     dimPtr = (long *) (&rank + 1);
  190.     for (i = 0; i < rank; ++i)
  191.     {
  192.     /* is this paranoia or what? */
  193.     dims[i] = (real) *(dimPtr++);
  194.     }
  195.     CArray2Array(dimsArray, dims);
  196.     SetVar(dataForm, DIMENSIONS, dimsArray); 
  197.  
  198.     /*Put in the bounds*/
  199.     for (i = 0; i < rank; ++i)
  200.     {
  201.     bounds[2 * i] = 0;
  202.     bounds[2 * i + 1] = dims[i] - 1;
  203.     }
  204.     boundsArray = NewRealArray(1, (long) 2 * rank);
  205.     if (!boundsArray)
  206.     {
  207.     fprintf(stderr, "memory!\n");
  208.     exit (-1);
  209.     }
  210.     CArray2Array(boundsArray, bounds);
  211.     SetVar(dataForm, BOUNDS, boundsArray);
  212.  
  213.     /* create the data array */
  214.     switch(rank)
  215.     {
  216.     case 2:
  217.         data = NewRealArray(2, (long) dims[0], (long) dims[1]);
  218.         theDataset = NewObject(data2DScalar, 0);
  219.         break;
  220.     case 3:
  221.         data = NewRealArray(3, (long) dims[0], (long) dims[1], (long) dims[2]);
  222.         theDataset = NewObject(data3DScalar, 0);
  223.         break;
  224.     }
  225.     if (!data || !theDataset)
  226.     {
  227.     fprintf(stderr, "memory!\n");
  228.     exit (-1);
  229.     }
  230.     CArray2Array(data, arr);
  231.  
  232.     /*Create the field*/
  233.     SetVar(theDataset, DATA, data);
  234.     SetVar(theDataset, DATAFORM, dataForm);
  235.     if (name && *name)
  236.     {
  237.     SetVar(theDataset, NAME, NewString(name));
  238.     }
  239.     else
  240.     {
  241.     SetVar(theDataset, NAME, NewString((rank == 3) ? "3-D Array" : "2-D Array"));
  242.     }
  243.     SetVar(theDataset, DIRECTORY, NewString("/scri4d/a/users/murray/sciancomp"));
  244.  
  245.     if(AdvertiseObject(theDataset))
  246.     {
  247.     return GetInt(GetVar(theDataset, NETWORKID));
  248.     }
  249.     return 0;
  250. }
  251.  
  252.  
  253. #ifdef PROTO
  254. int UpdateCArray(int netid, real *arr, int rank, ...)
  255. #else
  256. int UpdateCArray(netid, arr, rank)
  257. int netid;
  258. real *arr;
  259. int rank;
  260. #endif
  261. /* UpdateCArray replaces the old value of DATA with the new array. For this to
  262.  * work properly, the new data array MUST be of the same shape as the old one.
  263.  * ( but this is currently not checked)
  264.  */
  265. {
  266.     ObjPtr theDataset;
  267.     ObjPtr data;
  268.     real dims[23];
  269.     long *dimPtr;
  270.     int i;
  271.  
  272.     if (!libRunning)
  273.     {
  274.     fprintf(stderr, "AdvertiseCArray: SciAn library not initialized\n");
  275.     return 0;
  276.     }
  277.  
  278.     theDataset = FindPublishedObject(netid);
  279.  
  280.     if (! theDataset)
  281.     {
  282.     return 0;
  283.     }
  284.  
  285.     dimPtr = (long *) (&rank + 1);
  286.     for (i = 0; i < rank; ++i)
  287.     {
  288.         /* is this paranoia or what? */
  289.         dims[i] = (real) *(dimPtr++);
  290.     }
  291.  
  292.  
  293.     switch(rank)
  294.     {
  295.     case 2:
  296.         data = NewRealArray(2, (long) dims[0], (long) dims[1]);
  297.         break;
  298.     case 3:
  299.         data = NewRealArray(3, (long) dims[0], (long) dims[1], (long) dims[2]);
  300.         break;
  301.     }
  302.     CArray2Array(data, arr);
  303.     SetVar(theDataset, DATA, data);
  304.     return 1;
  305. }
  306.